/******************************************************************************

	COPYRIGHT(C) JONAS 'SORTIE' TERMANSEN 2009, 2010.

	This file is part of the Maxsi Library.

	Maxsi Library is free software: you can redistribute it and/or modify it
	under the terms of the GNU Lesser General Public License as published by
	the Free Software Foundation, either version 3 of the License, or (at your
	option) any later version.

	Maxsi Library is distributed in the hope that it will be useful, but
	WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
	or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
	License for more details.

	You should have received a copy of the GNU Lesser General Public License
	along with Maxsi Library. If not, see <http://www.gnu.org/licenses/>.

	Maxsi Library
	A powerful Cross-Platform C++ General Purpose Library that allows you to
	efficiently create high-performance and reliable applications.

	MaxsiWin7TaskBar.cpp
	Functions to enable features introduced by Win7 without breaking
	compatibility with previous versions of Windows.

******************************************************************************/

#include "MaxsiLibrary.h"

#ifdef Maxsi_Support_W32
#include <initguid.h>

BeginMaxsiNamespace

//#define Maxsi_Win7_Feature_Debug

ITaskbarList3*	g_ITaskBarList3		=	NULL;
DWORD			g_Win7TBC			=	(DWORD)-1;
#ifdef Maxsi_Win7_Feature_Debug
bool			Complained			=	false;
#endif
bool			TriedEnablingWin7	=	false;

DEFINE_GUID(CLSID_TaskbarList, 0x56fdf344, 0xfd6d, 0x11d0, 0x95, 0x8a, 0x00, 0x60, 0x97, 0xc9, 0xa0, 0x90);

bool	EnableWin7TaskBar(HWND hwnd)
{
	TriedEnablingWin7	=	true;

	if ( g_ITaskBarList3 ) { return true; }

	if ( CoCreateInstance(
		CLSID_TaskbarList, NULL, CLSCTX_ALL,
		IID_ITaskbarList3, (void**)&g_ITaskBarList3) == S_OK )
	{
#ifdef Maxsi_Win7_Feature_Debug
		MessageBoxA(hwnd,"It should work!","Win7sux",MB_OK);
#endif
	}
	else
	{
#ifdef Maxsi_Win7_Feature_Debug
		// We couldn't get the interface!
		MessageBoxA(hwnd,"Couldn't create interface IID_ITaskbarList3 in EnableWin7TaskBar()","Win7sux",MB_OK);
		ME_UNDEFINED_BEHAVIOR("Couldn't create interface IID_ITaskbarList3 in EnableWin7TaskBar()!");
#endif
		g_ITaskBarList3		=	NULL;
		return false;
	}

#ifdef Maxsi_Win7_Feature_Debug
	if ( g_ITaskBarList3 == NULL )
	{
		ME_DONT_COMPILE_RELEASE_BUILD();
		MessageBoxA(hwnd,"Something went horribly terribly wrong in EnableWin7TaskBar()","Win7sux",MB_OK);
	}
#endif

	return true;
}

bool	Win7TaskBarSetState(HWND hwnd, TBPFLAG tbpFlags)
{
	if ( TriedEnablingWin7 == false )
	{
		EnableWin7TaskBar(hwnd);
	}

	if ( g_ITaskBarList3 == NULL )
	{
#ifdef Maxsi_Win7_Feature_Debug
		if ( Complained == false )
		{
			MessageBoxA(hwnd,"Something went horribly terribly wrong in Win7TaskBarSetState()","Win7sux",MB_OK);
			Complained	=	true;
		}
#endif
		return false;
	}

	g_ITaskBarList3->SetProgressState(hwnd, tbpFlags);

	return true;
}

bool	Win7TaskBarSetProgress(HWND hwnd, ULONGLONG ullCompleted, ULONGLONG ullTotal)
{
	if ( TriedEnablingWin7 == false )
	{
		EnableWin7TaskBar(hwnd);
	}

	if ( g_ITaskBarList3 == NULL )
	{
#ifdef Maxsi_Win7_Feature_Debug
		if ( Complained == false )
		{
			MessageBoxA(hwnd,"Something went horribly terribly wrong in Win7TaskBarSetProgress()","Win7sux",MB_OK);
			Complained	=	true;
		}
#endif
		return false;
	}

	g_ITaskBarList3->SetProgressValue(hwnd, ullCompleted, ullTotal);

	return true;
}

EndMaxsiNamespace

#endif

